// 公共函数

/**
 * @description:  获取指定日期为多少秒、分钟、小时、天前，或者年-月-日的日期
 * @param {*} time 为传入的指定日期，如果time不传，则默认为当前时间
 */
export function formatBeforeDate(time) {
  let dt = ''
  let nowDt = ''
  let beforeTime = ''
  if (time) {
    dt = new Date(time)
    nowDt = new Date()
  } else {
    dt = new Date()
  }

  const dtYear = dt.getFullYear()
  const nowDtYear = nowDt.getFullYear()
  const dtMonth = dt.getMonth() + 1
  const nowDtMonth = nowDt.getMonth() + 1
  const dtDate = dt.getDate()
  const nowDtDate = nowDt.getDate()
  const dtHours = dt.getHours()
  const nowDtHours = nowDt.getHours()
  const dtMinutes = dt.getMinutes()
  const nowDtMinutes = nowDt.getMinutes()
  const dtSeconds = dt.getSeconds()
  const nowDtSeconds = nowDt.getSeconds()

  // const hh = dtHours >= 10 ? dtHours : '0' + dtHours
  // const mm = dtMinutes >= 10 ? dtMinutes : '0' + dtMinutes
  // const ss = dtSeconds >= 10 ? dtSeconds : '0' + dtSeconds

  if (dtYear === nowDtYear) {
    if (dtMonth === nowDtMonth) {
      if (dtDate === nowDtDate) {
        if (dtHours === nowDtHours) {
          if (dtMinutes === nowDtMinutes) {
            if (dtSeconds === nowDtSeconds) {
              beforeTime = '刚刚'
            } else {
              // 秒
              beforeTime = nowDtSeconds - dtSeconds + '秒前'
            }
          } else {
            // 分
            beforeTime = nowDtMinutes - dtMinutes + '分钟前'
          }
        } else {
          // 小时
          beforeTime = nowDtHours - dtHours + '小时前'
        }
      } else {
        // 天
        if (nowDtDate - dtDate === 1) {
          beforeTime = `昨天 ${dtHours}时${dtMinutes}分`
        } else if (nowDtDate - dtDate === 2) {
          beforeTime = '两天前'
        } else {
          beforeTime = `${dtMonth}月${dtDate}日`
        }
      }
    } else {
      // 月
      beforeTime = `${dtMonth}月${dtDate}日`
    }
  } else {
    beforeTime = `${dtYear}年${dtMonth}月${dtDate}日`
  }

  return beforeTime
}